import java.io.IOException;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.xml.bind.JAXBException;


public class TextHandling {
	public static String name=null;
	public static String nameFormatted=null;
    private final static String newline = "\n";
    public static String notes = "c.d.ef.g.a.b";
    public static boolean keySet = false;
    public static boolean tempoSet = false;
    public static boolean newLearn = true;
	public static void receive(String text) throws JAXBException, IOException{

//        Main.textGUI.textArea.setText("length = "+ ((Integer) text.length()).toString() + newline);
        text=text.trim();
        text=text.toLowerCase();
        int textLength = text.length();
        if (textLength>3){
          if (text.substring(0,4).equals("save")){
        	  if (newLearn){
            	  Main.xml.setCount(Main.stats.count);        		  
        	  }
        	  else{
            	  Main.xml.setCount(Main.stats.count+XML.getCount(nameFormatted));
        	  }
        	  Main.xml.setDuration(Main.stats.duration);
        	  Main.xml.setNotes(Main.stats.notes);
        	  Main.xml.setVolume(Main.stats.volume);
        	  Main.xml.writeStyle();
              Main.textGUI.textArea.setText("saving the style of "+name+newline);
          }
        }

        if (textLength>6){
        	if (text.substring(0,5).equals("learn")){
//        		Main.xml.learning=true;
        		nameFormatted = text.toLowerCase().substring(6).replace(' ', '_');
        		name = text.substring(6);
        		Main.xml.newStyle(nameFormatted);
        		boolean exists = Main.xml.readStyle(nameFormatted);
        		if (exists){
        			Main.stats.count=0;
        			Main.stats.learn=true;
        			Main.stats.volume=XML.getVolume(nameFormatted);
        			Main.stats.notes=XML.getNotes(nameFormatted);
        			Main.stats.duration=XML.getDuration(nameFormatted);
            		Main.textGUI.textArea.setText("I remember "+name+" ...continuing to learn his style"+newline);
            		newLearn=false;
        		}
        		else {
        			Main.stats.init();
            		Main.textGUI.textArea.setText("This one is new to me, learning the style of "+name+newline);
            		newLearn=true;
        		}
        		keySet = false;
        		tempoSet = false;
        		Main.textGUI.textArea.append("in what key?"+newline);
        	}
        }
	}
}
